#include "show.h"

static unsigned int t_now = 0,t_last = 0;
unsigned char TMAX = 30;
unsigned char TMIN = 20;
unsigned char TMAX_1 = 30;
unsigned char TMIN_1 = 20;

void show_t(void)
{
	static unsigned int t_count = 299;
	static unsigned char t_wait = 0;
	float T = 0;
	nixie_show(1,1);
	nixie_show(4,20);
	nixie_show(5,20);
	if(t_wait)
	{
	ds18b20_zhuanhuan();
	t_wait = !t_wait;
	t_count = 0;
	}
	if(++t_count == 300)
	{
	t_wait = !t_wait;
	T = ds18b20_read();
		if(t_last)
	if(t_now - (unsigned int)(T * 10) <=20 || (unsigned int)(T * 10) - t_now <= 20)
	t_now = (unsigned int)(T * 10);
	if(!t_last)
		t_now = (unsigned int)(T * 10);
		t_last = t_now;
	}
	nixie_show(7,t_now/100);
	nixie_show(8,(t_now/10%10));
}

void show_para(u8* s_mode)
{
	unsigned int count_s2 = 0;
	unsigned char s2_mode = 0,s1_mode = 1;
	u8 Key_nunmber = 0;
	nixie_show(1,2);
	TMAX_1 = TMAX;
	TMIN_1 = TMIN;
	while(Key_nunmber != 1)
	{
		Key_nunmber = r_key();
		if(Key_nunmber == 2)
			s1_mode = !s1_mode;
		if(Key_nunmber == 3 && !s1_mode)
			TMAX++;
		if(Key_nunmber == 4 && !s1_mode)
			TMAX--;
		if(Key_nunmber == 3 && s1_mode)
			TMIN++;
		if(Key_nunmber == 4 && s1_mode)
			TMIN--;
		if(++count_s2 == 800)
		{
			s2_mode = !s2_mode;
			count_s2 = 0;
		}
		if(!s2_mode)
		{
			if(!s1_mode)
			{
	nixie_show(4,TMAX/10);
	nixie_show(5,TMAX%10);
			}
			if(s1_mode)
			{
	nixie_show(7,TMIN/10);
	nixie_show(8,TMIN%10);
			}
		}
		if(s2_mode)
		{
			if(!s1_mode)
			{
	nixie_show(4,20);
	nixie_show(5,20);
			}
			if(s1_mode)
			{
	nixie_show(7,20);
	nixie_show(8,20);
			}
		}
	if(!s1_mode)
			{
	nixie_show(7,TMIN/10);
	nixie_show(8,TMIN%10);
	
			}
			if(s1_mode)
			{
	nixie_show(4,TMAX/10);
	nixie_show(5,TMAX%10);
			}
		if(t_now/10 > TMAX)
	{
		Latch(LED,0xfe);
		da_write(255/5*4);
	}
		if(TMAX >= t_now/10 && t_now/10 >= TMIN)
		{
			Latch(LED,0xfd);
			da_write(255/5*3);
		}
			if(t_now < TMIN)
			{
				Latch(LED,0xfb);
				da_write(255/5*2);
			}
	}
	*s_mode = 1;
	if(TMIN > TMAX)
	{
		TMAX = TMAX_1;
		TMIN = TMIN_1;
	}
}
	
void key_mode()
{
	u8 Key_nunmber = 0;
	unsigned char show_mode = 1;
	while(1)
	{
		Key_nunmber = r_key();
	if(Key_nunmber == 1)
		show_mode = !show_mode;
	if(show_mode)
		show_t();
	else
		show_para(&show_mode);
	if(t_now/10 > TMAX)
	{
		Latch(LED,0xfe);
		da_write(255/5*4);
	}
		if(TMAX >= t_now/10 && t_now/10 >= TMIN)
		{
			Latch(LED,0xfd);
			da_write(255/5*3);
		}
			if(t_now < TMIN)
			{
				Latch(LED,0xfb);
				da_write(255/5*2);
			}
}
		
}
	